/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef URL_JS_URLHELPER_H
#define URL_JS_URLHELPER_H

#include <string>
#include <string_view>

namespace OHOS::Url {

constexpr size_t DECIMAL_RADIX = 10;
constexpr size_t INT_SHIFT_SIZE = 4;
constexpr size_t HEX_RADIX = 16;
constexpr size_t HEX_PAIR_LENGTH = 2;
constexpr size_t PERCENT_ENCODING_LENGTH = 3;
constexpr size_t SHIFT_SIZE = 4;
constexpr size_t CHARS_LENGTH = 4;
constexpr char HEX_CHAR_MAP[] = "0123456789ABCDEF";
typedef std::pair<std::string, std::string> KeyValue;

// hostname, host
constexpr uint16_t URL_ENCODED_PERCENT_SIGN_CHARS[16] = {
    // 00    01       02       03       04       05       06       07
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 08    09       0A       0B       0C       0D       0E       0F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 10    11       12       13       14       15       16       17
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 18    19       1A       1B       1C       1D       1E       1F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 20    21       22       23       24       25       26       27
    0x0001 | 0x0000 | 0x0004 | 0x0000 | 0x0000 | 0x0020 | 0x0000 | 0x0000 |
        // 28    29       2A       2B       2C       2D       2E       2F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 30    31       32       33       34       35       36       37
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 38    39       3A       3B       3C       3D       3E       3F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x1000 | 0x0000 | 0x4000 | 0x0000,
    // 40    41       42       43       44       45       46       47
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 48    49       4A       4B       4C       4D       4E       4F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 50    51       52       53       54       55       56       57
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 58    59       5A       5B       5C       5D       5E       5F
        0x0000 | 0x0000 | 0x0000 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x0000,
    // 60    61       62       63       64       65       66       67
    0x0001 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 68    69       6A       6B       6C       6D       6E       6F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 70    71       72       73       74       75       76       77
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 78    79       7A       7B       7C       7D       7E       7F
        0x0000 | 0x0000 | 0x0000 | 0x0800 | 0x1000 | 0x2000 | 0x0000 | 0x8000,
    // 80    81       82       83       84       85       86       87
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 88    89       8A       8B       8C       8D       8E       8F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 90    91       92       93       94       95       96       97
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 98    99       9A       9B       9C       9D       9E       9F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // A0    A1       A2       A3       A4       A5       A6       A7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // A8    A9       AA       AB       AC       AD       AE       AF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // B0    B1       B2       B3       B4       B5       B6       B7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // B8    B9       BA       BB       BC       BD       BE       BF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // C0    C1       C2       C3       C4       C5       C6       C7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // C8    C9       CA       CB       CC       CD       CE       CF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // D0    D1       D2       D3       D4       D5       D6       D7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // D8    D9       DA       DB       DC       DD       DE       DF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // E0    E1       E2       E3       E4       E5       E6       E7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // E8    E9       EA       EB       EC       ED       EE       EF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // F0    F1       F2       F3       F4       F5       F6       F7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // F8    F9       FA       FB       FC       FD       FE       FF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000
};

// username, password {'%25':'%'}
constexpr uint16_t USERINFO_PERCENT_SIGN_CHARS[16] = {
    // 00    01       02       03       04       05       06       07
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 08    09       0A       0B       0C       0D       0E       0F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 10    11       12       13       14       15       16       17
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 18    19       1A       1B       1C       1D       1E       1F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 20    21       22       23       24       25       26       27
    0x0001 | 0x0000 | 0x0004 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 28    29       2A       2B       2C       2D       2E       2F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 30    31       32       33       34       35       36       37
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 38    39       3A       3B       3C       3D       3E       3F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x1000 | 0x0000 | 0x4000 | 0x0000,
    // 40    41       42       43       44       45       46       47
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 48    49       4A       4B       4C       4D       4E       4F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 50    51       52       53       54       55       56       57
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 58    59       5A       5B       5C       5D       5E       5F
        0x0000 | 0x0000 | 0x0000 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x0000,
    // 60    61       62       63       64       65       66       67
    0x0001 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 68    69       6A       6B       6C       6D       6E       6F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 70    71       72       73       74       75       76       77
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 78    79       7A       7B       7C       7D       7E       7F
        0x0000 | 0x0000 | 0x0000 | 0x0800 | 0x1000 | 0x2000 | 0x0000 | 0x8000,
    // 80    81       82       83       84       85       86       87
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 88    89       8A       8B       8C       8D       8E       8F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 90    91       92       93       94       95       96       97
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 98    99       9A       9B       9C       9D       9E       9F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // A0    A1       A2       A3       A4       A5       A6       A7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // A8    A9       AA       AB       AC       AD       AE       AF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // B0    B1       B2       B3       B4       B5       B6       B7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // B8    B9       BA       BB       BC       BD       BE       BF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // C0    C1       C2       C3       C4       C5       C6       C7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // C8    C9       CA       CB       CC       CD       CE       CF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // D0    D1       D2       D3       D4       D5       D6       D7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // D8    D9       DA       DB       DC       DD       DE       DF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // E0    E1       E2       E3       E4       E5       E6       E7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // E8    E9       EA       EB       EC       ED       EE       EF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // F0    F1       F2       F3       F4       F5       F6       F7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // F8    F9       FA       FB       FC       FD       FE       FF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000
};

// path {'%7C': '|', '%5B': '[', '%5D': ']', '%25': '%'} %5C '\\'
constexpr uint16_t PATH_PERCENT_SIGN_CHARS[16] = {
    // 00    01       02       03       04       05       06       07
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 08    09       0A       0B       0C       0D       0E       0F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 10    11       12       13       14       15       16       17
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 18    19       1A       1B       1C       1D       1E       1F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 20    21       22       23       24       25       26       27
    0x0001 | 0x0000 | 0x0004 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 28    29       2A       2B       2C       2D       2E       2F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 30    31       32       33       34       35       36       37
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 38    39       3A       3B       3C       3D       3E       3F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x1000 | 0x0000 | 0x4000 | 0x0000,
    // 40    41       42       43       44       45       46       47
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 48    49       4A       4B       4C       4D       4E       4F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 50    51       52       53       54       55       56       57
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 58    59       5A       5B       5C       5D       5E       5F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x1000 | 0x0000 | 0x4000 | 0x0000,
    // 60    61       62       63       64       65       66       67
    0x0001 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 68    69       6A       6B       6C       6D       6E       6F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 70    71       72       73       74       75       76       77
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 78    79       7A       7B       7C       7D       7E       7F
        0x0000 | 0x0000 | 0x0000 | 0x0800 | 0x0000 | 0x2000 | 0x0000 | 0x8000,
    // 80    81       82       83       84       85       86       87
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 88    89       8A       8B       8C       8D       8E       8F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 90    91       92       93       94       95       96       97
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 98    99       9A       9B       9C       9D       9E       9F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // A0    A1       A2       A3       A4       A5       A6       A7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // A8    A9       AA       AB       AC       AD       AE       AF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // B0    B1       B2       B3       B4       B5       B6       B7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // B8    B9       BA       BB       BC       BD       BE       BF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // C0    C1       C2       C3       C4       C5       C6       C7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // C8    C9       CA       CB       CC       CD       CE       CF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // D0    D1       D2       D3       D4       D5       D6       D7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // D8    D9       DA       DB       DC       DD       DE       DF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // E0    E1       E2       E3       E4       E5       E6       E7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // E8    E9       EA       EB       EC       ED       EE       EF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // F0    F1       F2       F3       F4       F5       F6       F7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // F8    F9       FA       FB       FC       FD       FE       FF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000
};

// hash, fragment {'%7C': '|', '%5B': '[', '%5D': ']', '%7B': '{', '%7D': '}', '%60': '`', '%25': '%'}
constexpr uint16_t FRAGMENT_PERCENT_SIGN_CHARS[16] = {
    // 00    01       02       03       04       05       06       07
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 08    09       0A       0B       0C       0D       0E       0F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 10    11       12       13       14       15       16       17
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 18    19       1A       1B       1C       1D       1E       1F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 20    21       22       23       24       25       26       27
    0x0001 | 0x0000 | 0x0004 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 28    29       2A       2B       2C       2D       2E       2F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 30    31       32       33       34       35       36       37
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 38    39       3A       3B       3C       3D       3E       3F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x1000 | 0x0000 | 0x4000 | 0x0000,
    // 40    41       42       43       44       45       46       47
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 48    49       4A       4B       4C       4D       4E       4F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 50    51       52       53       54       55       56       57
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 58    59       5A       5B       5C       5D       5E       5F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x1000 | 0x0000 | 0x4000 | 0x0000,
    // 60    61       62       63       64       65       66       67
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 68    69       6A       6B       6C       6D       6E       6F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 70    71       72       73       74       75       76       77
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 78    79       7A       7B       7C       7D       7E       7F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x8000,
    // 80    81       82       83       84       85       86       87
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 88    89       8A       8B       8C       8D       8E       8F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 90    91       92       93       94       95       96       97
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 98    99       9A       9B       9C       9D       9E       9F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // A0    A1       A2       A3       A4       A5       A6       A7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // A8    A9       AA       AB       AC       AD       AE       AF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // B0    B1       B2       B3       B4       B5       B6       B7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // B8    B9       BA       BB       BC       BD       BE       BF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // C0    C1       C2       C3       C4       C5       C6       C7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // C8    C9       CA       CB       CC       CD       CE       CF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // D0    D1       D2       D3       D4       D5       D6       D7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // D8    D9       DA       DB       DC       DD       DE       DF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // E0    E1       E2       E3       E4       E5       E6       E7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // E8    E9       EA       EB       EC       ED       EE       EF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // F0    F1       F2       F3       F4       F5       F6       F7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // F8    F9       FA       FB       FC       FD       FE       FF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000
};

// query {'%7C': '|', '%5B': '[', '%5D': ']', '%7B': '{', '%7D': '}', '%60': '`', '%5E': '^', '%25': '%'}
constexpr uint16_t QUERY_PERCENT_SIGN_CHARS[16] = {
    // 00    01       02       03       04       05       06       07
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 08    09       0A       0B       0C       0D       0E       0F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 10    11       12       13       14       15       16       17
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 18    19       1A       1B       1C       1D       1E       1F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 20    21       22       23       24       25       26       27
    0x0001 | 0x0000 | 0x0004 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 28    29       2A       2B       2C       2D       2E       2F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 30    31       32       33       34       35       36       37
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 38    39       3A       3B       3C       3D       3E       3F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x1000 | 0x0000 | 0x4000 | 0x0000,
    // 40    41       42       43       44       45       46       47
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 48    49       4A       4B       4C       4D       4E       4F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 50    51       52       53       54       55       56       57
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 58    59       5A       5B       5C       5D       5E       5F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x1000 | 0x0000 | 0x0000 | 0x0000,
    // 60    61       62       63       64       65       66       67
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 68    69       6A       6B       6C       6D       6E       6F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000,
    // 70    71       72       73       74       75       76       77
    0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 |
        // 78    79       7A       7B       7C       7D       7E       7F
        0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x8000,
    // 80    81       82       83       84       85       86       87
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 88    89       8A       8B       8C       8D       8E       8F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // 90    91       92       93       94       95       96       97
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // 98    99       9A       9B       9C       9D       9E       9F
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // A0    A1       A2       A3       A4       A5       A6       A7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // A8    A9       AA       AB       AC       AD       AE       AF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // B0    B1       B2       B3       B4       B5       B6       B7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // B8    B9       BA       BB       BC       BD       BE       BF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // C0    C1       C2       C3       C4       C5       C6       C7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // C8    C9       CA       CB       CC       CD       CE       CF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // D0    D1       D2       D3       D4       D5       D6       D7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // D8    D9       DA       DB       DC       DD       DE       DF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // E0    E1       E2       E3       E4       E5       E6       E7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // E8    E9       EA       EB       EC       ED       EE       EF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000,
    // F0    F1       F2       F3       F4       F5       F6       F7
    0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 |
        // F8    F9       FA       FB       FC       FD       FE       FF
        0x0100 | 0x0200 | 0x0400 | 0x0800 | 0x1000 | 0x2000 | 0x4000 | 0x8000
};

inline bool NeedEncode(const uint16_t codeMap[], const uint8_t asciiChar)
{
    return (codeMap[asciiChar >> INT_SHIFT_SIZE] & (1 << (asciiChar & 0x0F))) != 0;
}

inline std::uint8_t HexToDecimal(unsigned char hexChar)
{
    if (hexChar >= '0' && hexChar <= '9') {
        return hexChar - '0';
    } else if (hexChar >= 'A' && hexChar <= 'F') {
        return hexChar - 'A' + DECIMAL_RADIX;
    } else if (hexChar >= 'a' && hexChar <= 'f') {
        return hexChar - 'a' + DECIMAL_RADIX;
    } else {
        return 0;
    }
}

inline void AppendChars(std::string& result, size_t& i, size_t length, char* chars)
{
    for (size_t j = length; j-- > 0;) {
        result += chars[j];
        i += (HEX_PAIR_LENGTH + 1);
    }
}
inline bool PercentCharDecodable(const std::string_view inputString, size_t i, char& ch)
{
    if (inputString[i] == '%' && i + HEX_PAIR_LENGTH < inputString.size() && std::isxdigit(inputString[i + 1]) &&
        std::isxdigit(inputString[i + HEX_PAIR_LENGTH])) {
        ch = HexToDecimal(inputString[i + 1]) * HEX_RADIX + HexToDecimal(inputString[i + HEX_PAIR_LENGTH]);
        return true;
    }
    return false;
}

inline size_t GetCharLength(char ch)
{
    if ((ch & 0x80) == 0) {               // 0x80 mean 0XXXXXXX
        return 1;                         // 1 char length,0XXXXXXX
    } else if (((ch >> 5) ^ 0x6) == 0) {  // 5,0x6 mean begin with 110XXXXX
        return 2;                         // 2 char length,110XXXXX 10XXXXXX
    } else if (((ch >> 4) ^ 0xE) == 0) {  // 4,0xE mean begin with 1110XXXX
        return 3;                         // 3 char length,110XXXXX 10XXXXXX 10XXXXXX
    } else if (((ch >> 3) ^ 0x1E) == 0) { // 3,0x1E mean begin with 11110XXX
        return 4;                         // 4 char length,110XXXXX 10XXXXXX 10XXXXXX 10XXXXXX
    }
    return 0;
}

std::string EncodePercentEncoding(const std::string_view inputString, const uint16_t codeMap[]);
std::string DecodePercentEncoding(const std::string_view inputString);
void StringAnalyzing(std::string_view inputString, std::vector<KeyValue>& params);

} // namespace OHOS::Url
#endif // URL_JS_URLHELPER_H